package com.mdp.im.core.publish.action.impl.msgaction;

import com.alibaba.fastjson.JSONObject;
import com.mdp.im.core.entity.ImPrichatMessage;
import com.mdp.im.core.entity.ImUserFrend;
import com.mdp.im.core.publish.api.action.PrichatMsgAction;
import com.mdp.im.core.service.ImPrichatMessageService;
import com.mdp.im.core.service.ImUserFrendService;
import com.mdp.im.core.utils.msg.MsgAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
/**
 * msgId:xxxx,
 * groupId:xxxxx,
 * msgType:prichat,
 * action:newMessage,
 * sendUserid:xxx,
 * toUserid:xxxxx,
 * sourceMsgId:xxxxx,///agreeJoinFrend/refuseJoinFrend/时需要
 * msgAction: inviteJoinFrend/agreeJoinFrend/refuseJoinFrend/delJoinFrend/blackJoinFend/removeBlackFrend 邀请/同意/拒绝/删除/拉黑/拉出黑名单
 * 
 * @author qqkj
 *
 */
public class InviteJoinFrendMsgAction implements PrichatMsgAction {
	
	@Autowired
	ImUserFrendService frendService;
	
	@Autowired
	ImPrichatMessageService prichatMessageService;
	
	@Override
	public void doAction(JSONObject message) {
		String msgAction=message.getString("msgAction");
		String sendUserid=message.getString("sendUserid");
		String toUserid=message.getString("toUserid");
		if(MsgAction.prichat_agreeJoinFrend.equals(msgAction)) {
			
			String sourceMsgId=message.getString("sourceMsgId");
			ImPrichatMessage sourceMessageParam=new ImPrichatMessage();
			sourceMessageParam.setMsgId(sourceMsgId);
			sourceMessageParam.setMsgId(sourceMsgId);
			ImPrichatMessage sourceMessage = prichatMessageService.selectOneObject(sourceMessageParam);
			if(sourceMessage==null||!sourceMessage.getSendUserid().equals(toUserid)) {
				return;
			}else {
				ImUserFrend myFrend=new ImUserFrend();
				myFrend.setId(sendUserid+"_"+toUserid);
				myFrend.setBlack("0");
				myFrend.setFrendUserid(toUserid);
				myFrend.setUserid(sendUserid);
				myFrend.setFrendUsername(sourceMessage.getSendUsername());
				myFrend.setJoinTime(new Date());
				myFrend.setLastUpdateTime(new Date());
				frendService.insertOrUpdate(myFrend);
				
				ImUserFrend hisFrend=new ImUserFrend();
				hisFrend.setId(toUserid+"_"+sendUserid);
				hisFrend.setBlack("0");
				hisFrend.setFrendUserid(sendUserid);
				hisFrend.setUserid(toUserid);
				hisFrend.setFrendUsername(message.getString("sendUsername"));
				hisFrend.setJoinTime(new Date());
				hisFrend.setLastUpdateTime(new Date());
				frendService.insertOrUpdate(hisFrend);
				sourceMessageParam.setMsgState("actionEnd");
				prichatMessageService.updateSomeFieldByPk(sourceMessageParam);
			}
			
		}else if(MsgAction.prichat_refuseJoinFrend.equals(msgAction)) {
			String sourceMsgId=message.getString("sourceMsgId");
			if(StringUtils.isEmpty(sourceMsgId)) {
				return;
			}
			ImPrichatMessage sourceMessageParam=new ImPrichatMessage();
			sourceMessageParam.setMsgId(sourceMsgId);
			sourceMessageParam.setMsgId(sourceMsgId);
			sourceMessageParam.setMsgState("actionEnd");
			prichatMessageService.updateSomeFieldByPk(sourceMessageParam);
		}else if(MsgAction.prichat_inviteJoinFrend.equals(msgAction)) {
			ImUserFrend myFrend=new ImUserFrend();
			myFrend.setId(sendUserid+"_"+toUserid); 
			ImUserFrend frend=frendService.selectOneObject(myFrend);
			if(frend!=null) {
				message.put("store", "0");
			}
		}else if(MsgAction.prichat_delJoinFrend.equals(msgAction)) {
			ImUserFrend myFrend=new ImUserFrend();
			myFrend.setId(sendUserid+"_"+toUserid); 
			myFrend.setBlack("1");
			myFrend.setLastUpdateTime(new Date());
			frendService.updateSomeFieldByPk(myFrend);
			ImUserFrend hisFrend=new ImUserFrend();
			hisFrend.setId(toUserid+"_"+sendUserid); 
			hisFrend.setBlack("1");
			hisFrend.setLastUpdateTime(new Date());
			frendService.updateSomeFieldByPk(hisFrend);
			 
		}else if(MsgAction.prichat_blackJoinFrend.equals(msgAction)) {
			ImUserFrend myFrend=new ImUserFrend();
			myFrend.setId(sendUserid+"_"+toUserid); 
			myFrend.setBlack("1");
			myFrend.setLastUpdateTime(new Date());
			frendService.updateSomeFieldByPk(myFrend);  
		}else if(MsgAction.prichat_removeBlackFrend.equals(msgAction)) {
			ImUserFrend myFrend=new ImUserFrend();
			myFrend.setId(sendUserid+"_"+toUserid); 
			myFrend.setBlack("0");
			myFrend.setLastUpdateTime(new Date());
			frendService.updateSomeFieldByPk(myFrend);  
		}
	}

	@Override
	public boolean suportMsgAction(String msgAction) {
		// TODO Auto-generated method stub
		return MsgAction.prichat_inviteJoinFrend.equals(msgAction)|| MsgAction.prichat_agreeJoinFrend.equals(msgAction)|| MsgAction.prichat_refuseJoinFrend.equals(msgAction)|| MsgAction.prichat_delJoinFrend.equals(msgAction)|| MsgAction.prichat_blackJoinFrend.equals(msgAction)|| MsgAction.prichat_removeBlackFrend.equals(msgAction);
	}

}
